package com.liezh.singletons;

/**
 * 经典的饱汉模式
 * 双重校验锁（DCL，即 double-checked locking）
 * 添加volatile是保证内存的可见性，在高并发场景下不会重复创建，保证线程安全
 * @author liezh
 * @date 2021-8-31
 */
public class DCLLazySingletonRpcClient extends RpcClient {

    private volatile static DCLLazySingletonRpcClient rpcClient;

    private DCLLazySingletonRpcClient() {}

    private DCLLazySingletonRpcClient(String ip, String port, int poolSize, int timeout) {
        super(ip, port, poolSize, timeout);
    }

    public static RpcClient getRpcClient() {
        if (rpcClient == null) {
            synchronized (RpcClient.class) {
                if (rpcClient == null) {
                    rpcClient = new DCLLazySingletonRpcClient("127.0.0.1", "9090", 5, 5000);
                }
            }
        }
        return rpcClient;
    }

    public static void main(String[] args) {
        RpcClient rpcClient = getRpcClient();
        rpcClient.send("hello world!!");
    }
}
